

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="CodeHope">
  <meta name="author" content="CodeHope">
  <meta name="keywords" content="希望">
  <title>Vue-使用Vuex的使用习惯 - CodeHope</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      
        
          
          
          
        
        <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs@1.22.0/themes/prism-tomorrow.min.css" />
      
      
        <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs@1.22.0/plugins/line-numbers/prism-line-numbers.min.css" />
      
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"quancundexiwang.wang","root":"/","version":"1.8.7","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":true,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"onlypost":true},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"Mi65hxq7VAFUDwOLeIGAOgiV-gzGzoHsz","app_key":"hMuhiD4FRqhns4giqLiEH9HG","server_url":null}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
  <header style="height: 80vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>CodeHope</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                村头
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                我
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('http://qiniuyun.quancundexiwang.wang/vuebg.png') repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="Vue-使用Vuex的使用习惯">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-09-16 11:00" pubdate>
        2020年9月16日 上午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.2k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      16
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
            <div class="scroll-down-bar">
              <i class="iconfont icon-arrowdown"></i>
            </div>
          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Vue-使用Vuex的使用习惯</h1>
            
            <div class="markdown-body">
              <h2 id="vuex-模块管理"><a href="#vuex-模块管理" class="headerlink" title="vuex 模块管理"></a>vuex 模块管理</h2><h2 id="1-基本使用"><a href="#1-基本使用" class="headerlink" title="1.基本使用"></a>1.基本使用</h2><blockquote>
<p>我们在使用vuex经常根据业务把各种数据分模块管理</p>
</blockquote>
<blockquote>
<p>每一个模块是一个对象，有自己的 <code>state,mutations,getters,actions</code></p>
</blockquote>
<p>我们在vuex的入口文件中<code>/store/index.js</code>,<br>需要使用modules来接受同的模块，这些模块都是一个个的store模块，</p>
<p><code>/store/index.js</code></p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js">modules<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
      baseData<span class="token punctuation">,</span> userInfo<span class="token punctuation">,</span> saveOtherUser<span class="token punctuation">,</span> productions<span class="token punctuation">,</span>student_contract<span class="token punctuation">,</span>studentInfo
  <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p><code>store/module/baseData.js</code></p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js"><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">&#123;</span>
  state<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
    baseInfo<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        userName<span class="token operator">:</span> <span class="token string">""</span><span class="token punctuation">,</span>
        age<span class="token operator">:</span> <span class="token string">""</span><span class="token punctuation">,</span>
        address<span class="token operator">:</span> <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  mutations<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
    <span class="token constant">SAVE_USER_ID</span><span class="token punctuation">(</span><span class="token parameter">state<span class="token punctuation">,</span> params</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
      state<span class="token punctuation">.</span>baseInfo <span class="token operator">=</span> params<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  actions<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">async</span> <span class="token function">handleLogin</span><span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">&#123;</span> commit <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>params</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> 
        <span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'SAVE_USER_ID'</span><span class="token punctuation">,</span>params<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>useage使用</code></p>
<div class="code-wrapper"><pre class="line-numbers language-vue" data-language="vue"><code class="language-vue">&lt;template&gt;
  &lt;div id&#x3D;&quot;app&quot;&gt;
    &lt;!-- &#123;&#123;userInfo&#125;&#125; --&gt;
    &#123;&#123;baseInfo&#125;&#125;
    &lt;button @click&#x3D;&quot;changeBaseInfo&quot;&gt;changeBaseInfo&lt;&#x2F;button&gt;
    &lt;router-view&#x2F;&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;template&gt;

&lt;script&gt;
import &#123;mapState&#125; from &quot;vuex&quot;
export default &#123;
  computed:&#123;
    &#x2F;&#x2F; ...mapState([&#39;baseInfo&#39;]),
    &#x2F;&#x2F; ...mapState(&#123;
    &#x2F;&#x2F;   baseInfo:state&#x3D;&gt;state.baseInfo
    &#x2F;&#x2F; &#125;)
    ...mapState(&#123;baseInfo:&quot;baseInfo&quot;&#125;)
  &#125;,
  methods: &#123;
    ...mapActions([&#39;handleLogin&#39;]),
    changeBaseInfo()&#123;
      this.handleLogin(&#123;
            userName: &quot;2&quot;,
            age: &quot;3&quot;,
            address: &quot;4&quot;,
        &#125;)
    &#125;
  &#125;,
  created()&#123;
    console.log(this.baseInfo)
  &#125;
&#125;
&lt;&#x2F;script&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20200916121138.gif" srcset="/img/loading.gif"><br>这就可以在组件内正常使用了</p>
<h2 id="2-namespaced命名空间"><a href="#2-namespaced命名空间" class="headerlink" title="2.namespaced命名空间"></a>2.namespaced命名空间</h2><blockquote>
<p>当我们多人协作开发，store比较多，难免有一些方法 属性名重复，使用的时候容易造成混乱，<code>namespaced</code>就很好的解决了这个问题.</p>
</blockquote>
<p>我们定义一个<code>authInfo的store</code></p>
<p>里面写上和上面的baseInfo一样的actions方法 和 state属性，然后通过mapactions拿出并调用看看究竟执行哪一个</p>
<p><code>baseInfo.js</code></p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js">actions<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
      <span class="token keyword">async</span> <span class="token function">handleLogin</span><span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">&#123;</span> commit <span class="token punctuation">&#125;</span><span class="token punctuation">,</span> params</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
          console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'调用的baseInfo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>params<span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'SAVE_USER_ID'</span><span class="token punctuation">,</span> params<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>authInfo.js</code></p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js">actions<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
      <span class="token keyword">async</span> <span class="token function">handleLogin</span><span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">&#123;</span> commit <span class="token punctuation">&#125;</span><span class="token punctuation">,</span> params</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
          console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'调用的authInfo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>params<span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'SAVE_USER_ID'</span><span class="token punctuation">,</span> params<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>store/index.js</code></p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js">modules<span class="token operator">:</span><span class="token punctuation">&#123;</span>
  baseInfo<span class="token punctuation">,</span>authInfo
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p>使用结果，这个结果和模块引入顺序有关系，匹配最后引入的模块中的方法</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20200916122623.png" srcset="/img/loading.gif"></p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js">modules<span class="token operator">:</span><span class="token punctuation">&#123;</span>
   authInfo<span class="token punctuation">,</span>baseInfo
 <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20200916122859.png" srcset="/img/loading.gif"></p>
<p>我们也不可能通过调节位置来控制，所以这个时候就需要我们的<code>namespaced</code>属性来帮助我们</p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js"><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">&#123;</span>
    namespaced<span class="token operator">:</span><span class="token boolean">true</span><span class="token punctuation">,</span>
    state<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
    <span class="token operator">...</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    mutations<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
     <span class="token operator">...</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    actions<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
     <span class="token operator">...</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>这样的话这个模块就会根据模块文件名称划分，<br>我们在使用的时候就可以传一个模块名称来区分，如果不传名称的话，vuex此时是找不到去哪一个模块的<br><img src="http://qiniuyun.quancundexiwang.wang/20200916124736.png" srcset="/img/loading.gif"></p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js"><span class="token operator">...</span><span class="token function">mapActions</span><span class="token punctuation">(</span><span class="token string">'baseInfo'</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string">'handleLogin'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20200916122859.png" srcset="/img/loading.gif"></p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js"><span class="token operator">...</span><span class="token function">mapActions</span><span class="token punctuation">(</span><span class="token string">'authInfo'</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string">'handleLogin'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20200916122623.png" srcset="/img/loading.gif"></p>
<p><code>mapState,mapGetters</code>也是一样的</p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js"><span class="token operator">...</span><span class="token function">mapState</span><span class="token punctuation">(</span><span class="token string">'baseInfo'</span><span class="token punctuation">,</span><span class="token punctuation">&#123;</span>baseInfo<span class="token operator">:</span><span class="token string">"baseInfo"</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20200916125315.png" srcset="/img/loading.gif"></p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js"><span class="token operator">...</span><span class="token function">mapState</span><span class="token punctuation">(</span><span class="token string">'authInfo'</span><span class="token punctuation">,</span><span class="token punctuation">&#123;</span>baseInfo<span class="token operator">:</span><span class="token string">"baseInfo"</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20200916125402.png" srcset="/img/loading.gif"></p>
<h2 id="3-自己最常用的项目规划方式-晚点更新"><a href="#3-自己最常用的项目规划方式-晚点更新" class="headerlink" title="3.自己最常用的项目规划方式 #晚点更新"></a>3.自己最常用的项目规划方式 #晚点更新</h2><h3 id="1-解决导入模块较多的问题"><a href="#1-解决导入模块较多的问题" class="headerlink" title="1.解决导入模块较多的问题"></a>1.解决导入模块较多的问题</h3><p>上面在<code>store/index.js</code> 引入模块的方式</p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js">modules<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
      baseData<span class="token punctuation">,</span> userInfo<span class="token punctuation">,</span> saveOtherUser<span class="token punctuation">,</span> productions<span class="token punctuation">,</span>student_contract<span class="token punctuation">,</span>studentInfo
  <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p>我们每一个模块都要import  然后放入 modules对象中，如果很多的话就不太方便，所以我们使用<code>自动注册</code>store中的模块。</p>
<p><font style="color:red">首先改变我们的模块文件定义方式</font></p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js"><span class="token comment">//这里将常用的字符串使用常量定义一下，方便使用</span>
<span class="token keyword">const</span> <span class="token constant">SAVE_USER_INFO</span>  <span class="token operator">=</span> <span class="token string">"saveUserbaseInfo"</span><span class="token punctuation">;</span>


<span class="token comment">//将每一个部分使用常量定义接受</span>
<span class="token keyword">const</span> state <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
    baseInfo<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
        userName<span class="token operator">:</span> <span class="token string">""</span><span class="token punctuation">,</span>
        age<span class="token operator">:</span> <span class="token string">""</span><span class="token punctuation">,</span>
        address<span class="token operator">:</span> <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">const</span> getters <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
    <span class="token function-variable function">userInfo</span><span class="token operator">:</span> <span class="token parameter">state</span> <span class="token operator">=></span> state<span class="token punctuation">.</span>baseInfo
<span class="token punctuation">&#125;</span>
<span class="token keyword">const</span> mutations <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
    <span class="token punctuation">[</span><span class="token constant">SAVE_USER_INFO</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter">state<span class="token punctuation">,</span> params</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        state<span class="token punctuation">.</span>baseInfo <span class="token operator">=</span> params<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">const</span> actions <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
    <span class="token function">handleAsyncSaveUsers</span><span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">&#123;</span> commit <span class="token punctuation">&#125;</span><span class="token punctuation">,</span> params</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token function">commit</span><span class="token punctuation">(</span><span class="token constant">SAVE_USER_INFO</span><span class="token punctuation">,</span> params<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">&#123;</span> <span class="token comment">//导出</span>
    namespaced<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
    state<span class="token punctuation">,</span>
    mutations<span class="token punctuation">,</span>
    getters<span class="token punctuation">,</span>
    actions<span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><font style="color:red">下一步最重要</font></p>
<p>我们在<code>modules/</code>下面放一个工具文件 index.js 用于将当前目录下的模块处理成一个<code>modules</code>对象，这样我们只需要在<code>store/index</code> 里面的modules 引入这个<code>modules</code>对象就可以了</p>
<p><font style="color:red">使用require.context</font></p>
<p>可以使用 require.context() 方法来创建自己的（模块）上下文，这个方法有 3 个参数：</p>
<p>要搜索的文件夹目录<br>是否还应该搜索它的子目录，<br>以及一个匹配文件的正则表达式。</p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js"><span class="token comment">/** @format */</span>

<span class="token keyword">const</span> files <span class="token operator">=</span> require<span class="token punctuation">.</span><span class="token function">context</span><span class="token punctuation">(</span><span class="token string">'./'</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\.js$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span>
<span class="token comment">// 1. 读取当前文件夹，不读取下一级，匹配.js文件</span>
<span class="token keyword">const</span> modules <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
files<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">key</span> <span class="token operator">=></span> <span class="token punctuation">&#123;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>key <span class="token operator">!==</span> <span class="token string">'./index.js'</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token comment">//过滤这个工具js文件</span>
    <span class="token comment">//将.js 和 ./替换成空字符  ./ShopStore.js ==> ShopStore</span>
    <span class="token keyword">const</span> moduleName <span class="token operator">=</span> key<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(\.js|\.\/)</span><span class="token regex-delimiter">/</span><span class="token regex-flags">gi</span></span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span> 
    modules<span class="token punctuation">[</span>moduleName<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">files</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">.</span>default
  <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> modules<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>这一步就相当于把所有的模块做成了这种形式</p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js"><span class="token punctuation">&#123;</span>
  moduleName1<span class="token operator">:</span><span class="token punctuation">&#123;</span>
      <span class="token operator">...</span><span class="token punctuation">.</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  moduleName2<span class="token operator">:</span><span class="token punctuation">&#123;</span>
      <span class="token operator">...</span><span class="token punctuation">.</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  moduleName3<span class="token operator">:</span><span class="token punctuation">&#123;</span>
      <span class="token operator">...</span><span class="token punctuation">.</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  moduleName4<span class="token operator">:</span><span class="token punctuation">&#123;</span>
      <span class="token operator">...</span><span class="token punctuation">.</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>我们就可以直接使用modules</p>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js"><span class="token keyword">import</span> Vue <span class="token keyword">from</span> <span class="token string">'vue'</span>
<span class="token keyword">import</span> Vuex <span class="token keyword">from</span> <span class="token string">'vuex'</span>
<span class="token keyword">import</span> modules <span class="token keyword">from</span> <span class="token string">"./modules"</span>

Vue<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>Vuex<span class="token punctuation">)</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">new</span> <span class="token class-name">Vuex<span class="token punctuation">.</span>Store</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
  state<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  mutations<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  actions<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  modules<span class="token operator">:</span>modules
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>这样你每次新增模块就不用手动引入了，多人开发最好都要开启自己的命名空间，这样引入使用的时候不容易混乱，项目就很清晰。</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/vue%E6%A1%86%E6%9E%B6/">vue框架</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/vue-%E4%BD%BF%E7%94%A8vuex%E7%9A%84%E4%BD%BF%E7%94%A8%E4%B9%A0%E6%83%AF/">vue-使用vuex的使用习惯</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/posts/55586/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Element-Ui框架的小坑记</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/posts/53420/">
                        <span class="hidden-mobile">Git协作常用操作</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments">
                
                
  <div id="vcomments"></div>
  <script type="text/javascript">
    Fluid.utils.waitElementVisible('vcomments', function() {
      Fluid.utils.createScript('https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js', function () {
        new Valine({
          el: "#vcomments",
          app_id: "Mi65hxq7VAFUDwOLeIGAOgiV-gzGzoHsz",
          app_key: "hMuhiD4FRqhns4giqLiEH9HG",
          placeholder: "说点什么",
          path: window.location.pathname,
          avatar: "retro",
          meta: ["nick","mail","link"],
          pageSize: "10",
          lang: "zh-CN",
          highlight: false,
          recordIP: false,
          serverURLs: "",
        });
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the
    <a target="_blank" href="https://valine.js.org" rel="nofollow noopener noopener">comments powered by Valine.</a>
  </noscript>


              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
    

  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        豫ICP备20009912号-1
      </a>
    </span>
    
  </div>


  
</footer>

<!-- SCRIPTS -->

  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":true,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    
      <script  src="/js/lazyload.js" ></script>
    
  



  
    
  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.0/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.0/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "http://qiniuyun.quancundexiwang.wang/xmlSearch.xml";
      var inputArea = document.querySelector("#local-search-input");
      inputArea.onclick = function () {
        searchFunc(path, 'local-search-input', 'local-search-result');
        this.onclick = null
      }
    })()
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>



</body>
</html>
